package exp1st;

public class LinearNodeList<T> {

    LinearNode head;
    LinearNode tail;

    public LinearNodeList() {
        head = null;
    }

    public void add(T elem) {
        LinearNode node = new LinearNode(elem);
        LinearNode current;

        if (head == null)
            head = node;
        else {
            current = head;
            while (current.getNext() != null) {
                current = current.getNext();
            }
            current.setNext(node);
        }
    }

    public void insert(int index, T elem) {
        LinearNode node = new LinearNode(elem);
        LinearNode current = head;
        if (index != 0) {
            for (int i = 0; i < index - 1; i++) {
                current = current.getNext();
            }
            node.setNext(current.getNext());
            current.setNext(node);
        }
        else {
            head = node;
            head.setNext(current);
        }
    }

    public void delete(int index) {
        LinearNode node = head;
        if (index != 0) {
            for (int i = 0; i < index - 1; i++) {
                node = node.next;
            }
            node.next = node.next.next;
        }
        else
            head = head.next;
    }

    public int size() {
        LinearNode node = head;
        int result = 1;
        if (head == null)
            return 0;
        else {
            while (node.next != null) {
                node = node.next;
                result++;
            }
            return result;
        }
    }


    @Override
    public String toString() {
        LinearNode current;
        current = head;

        String str = "[";
        if (head == null)
            str += "  ";
        else {
            str += head.getElement() + ", ";
            while (current.getNext() != null) {
                current = current.getNext();
                str += current.getElement() + ", ";
            }
        }
        str = str.substring(0, str.length()-2);
        str += "]";

        return str;
    }

    public void selectionSort() {
        tail = null;
        LinearNode current = head;
        LinearNode smallPre;
        LinearNode min;

        while (current != null) {
            min = current;
            smallPre = getMinnode(current);
            if (smallPre != null) {
                min = smallPre.next;
                smallPre.next = min.next;
            }
            current = current == min ? current.next : current;
            if (tail == null) {
                head = min;
            } else {
                tail.next = min;
            }
            tail = min;

        }
    }
    public static LinearNode getMinnode(LinearNode head){

        LinearNode MIN = null;
        LinearNode min = head;
        LinearNode pre = head;
        LinearNode current = head.next;

        while(current != null){
            if(Integer.parseInt(String.valueOf(current.getElement())) < Integer.parseInt(String.valueOf(min.getElement())))
            {
                MIN = pre;
                min = current;
            }
            pre = current;
            current = current.next;
        }
        return MIN;
    }


}
